/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hue;

import java.io.ByteArrayOutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hue
 */
public class InitAdf extends OpenURL {
    
    @Override
    public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        super.process(request, response);
        
        List<String> alCookies = (List<String>) request.getSession().getAttribute(COOKIES_IN_SESSION);
        if(alCookies != null) {
            alCookies.clear();
        }
        
        String url = SERVER_URL + request.getRequestURI();

        ByteArrayOutputStream baOut = new ByteArrayOutputStream();
        httpGet(request, response, url, baOut);
        
        String content = baOut.toString();
        baOut.close();

        String adfImages = "http://cdn.adf.ly/images/";
        content = replace(content, adfImages, "");
        
        StringBuilder sb = new StringBuilder();
        sb.append(content);
        sb.append("\r\n<script>\r\n");
        sb.append("    document.getElementById('bottom').style.display = 'none';\r\n");
        sb.append("    function clickSkipAdButton() {\r\n");
        sb.append("        document.getElementById('skip_button').click();\r\n");
        sb.append("    }\r\n");
        sb.append("</script>\r\n");
        
        response.getOutputStream().write(sb.toString().getBytes());
    }
}
